/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyectoso;
import java.util.concurrent.Semaphore;
/**
 *
 * @author Christian
 */
public class Adult implements Runnable{
    int isla;
    Bote b;
    int persona;
    Arena ar;
    int childrenintheislandM;
    int childrenintheislandO;
    int peopleinislandM;
    Semaphore sem;
    boolean flagbote;
    Avisar av;
    Adult(int is, Bote b1, Arena a, Semaphore s,Avisar avisar){
        this.isla=is;
        this.b=b1;
        this.persona=1;
        this.ar=a;
        this.sem=s;
        this.flagbote=false;
        this.av=avisar;
    }
    public void run(){
        this.ar.Escribir(isla, persona);
        if(ar.GetChildren(isla)==0)
            ar.Wait();
        while(RevisarCondiciones()){
            if (ar.GetChildren(isla)<=1){
                this.Viaja1();
            }
            if(isla==1 && ar.GetChildren(isla)==0){
                this.Viaja1();
            }
        }
        if(childrenintheislandO==0)
            av.DarAviso();
    }
    public void Viaja1(){
        try{
            sem.acquire(2);
            b.TomarBoteAdult(this);
            b.ViajarAdulto(this);
            sem.release(2);
        }
        catch(Exception e){
                
        }
    }
    public boolean RevisarCondiciones(){
        this.Leer();
        if(isla==0)
            return true;
        else
            return false;
    }
    public void ChildrenInIsland(){
        if(this.isla==0)
            this.childrenintheislandO=this.ar.GetChildren(this.isla);
        else{
            this.childrenintheislandM=this.ar.GetChildren(this.isla);
            this.peopleinislandM=this.ar.GetPeople(isla);
        }
    }
    public void Leer(){
        if (isla==0)
            childrenintheislandO=ar.GetChildren(isla);
        else
            childrenintheislandM=ar.GetChildren(isla);
    }
    public boolean GetFlag(){
        return flagbote;
    }
    public void SetFlag(boolean f){
        this.flagbote=f;
    }
    public int GetIsla(){
        return isla;
    }
    public void SetIsla(int is){
        this.isla=is;
    }
    public int GetPerson(){
        return persona;
    }
}
